
global user "dveracossio"
global dirdata "C:\Users\\$user\Dropbox\CCT BJP\data"
global dir1  "$dirdata\Final data"
global dir2  "$dirdata\Results\graphs"
global dir3  "$dirdata\Results\"
cd "$dir3"

use "$dir1\pooled_hh.dta", clear
keep if tau>-5  
g age=age_m if hhmale==0
replace age=age_p if hhmale==1
g spanish=spanish_m if hhmale==0
replace spanish=spanish_p if hhmale==1

g Post=tau>=0
egen w_hoursw_head=rowtotal(w_hoursw_p w_hoursw_m)
g work_head=w_hoursw_head>0
replace work_head=. if work_p==. & work_m==.
g informal4_head=informal4_m==1 | informal4_p==1
egen w_insumos_head=rowtotal(w_insumos_p w_insumos_m)
egen w_salarios_head=rowtotal(w_salarios_p w_salarios_m)
egen w_ylabor_head=rowtotal(w_ylabor_m w_ylabor_p)
egen w_rev_head=rowtotal(w_rev_m w_rev_p)
egen w_costs_head=rowtotal(w_insumos_head w_salarios_head)
egen w_costs_m=rowtotal(w_insumos_m w_salarios_m)
egen w_costs_p=rowtotal(w_insumos_p w_salarios_p)
egen w_costs_t=rowtotal(w_insumos_t w_salarios_t)
g any_costs_head=w_costs_head>0
g any_costs_p=w_costs_p>0
g any_costs_m=w_costs_m>0
g any_costs_t=w_costs_t>0
replace any_costs_head=. if w_costs_head==.
replace any_costs_p=. if w_costs_p==.
replace any_costs_m=. if w_costs_m==.
replace any_costs_t=. if w_costs_t==.

local nwork_lab "# of working adults"
local w_hoursw_t_lab "Hours/week"
local work_head_lab "Worked last week"
local w_hoursw_head_lab "Hours/week"
local work_m_lab "Worked last week"
local work_p_lab "Worked last week"
local w_hoursw_m_lab "Hours/week "
local w_hoursw_p_lab "Hours/week "
local parm Post c.Post#c.Top
local replace replace
replace nbi=1-nbi
egen munXy=group(municipio year)
******** Heteregeneity by Access to Financial services:

g Top=1 if q_fpc2005>=3 & q_fpc2005!=.
replace Top=0 if q_fpc2005<=2

global covs1 "  age spanish schooling hhmale urban np nchild5 age_sac  c.age_sac#i.year c.np#i.year  c.urba#i.year i.departamento#i.year i.year nbi c.(nbi)#c.Post c.(nbi)#i.year i.min_schooling#c.(nbi)"
local spec1  Post c.Post#c.Top Top c.Top#i.year i.min_schooling i.min_schooling#c.Top  $covs1  
local sample1 "(min_schooling>=1 & min_schooling<9)"

local replace replace
foreach x in  work_head w_hoursw_head w_ylabor_head informal4_head w_rev_head w_costs_head any_costs_head {
sum `x'  [w=factor] if tau==-1 & `sample1' & Top==0
local mean_dv=r(mean)
areg `x'  `spec1' [w=factor]  if `sample1',a(departamento)cluster(municipio)
lincom _b[Post]+_b[c.Post#c.Top]
local bhigh=`r(estimate)'
test Post+c.Post#c.Top=0
local phigh=`r(p)'
outreg2 using "$dir3\PTable5r", excel `replace' ctitle(``x'_lab') keep(`parm' c.nbi#c.Post) less(0) nocons  bdec(3) cttop(`top`y'') adds(Clusters, e(N_clust),Mean DV, `mean_dv', Effect High Access, `bhigh', P-val High Access, `phigh' ) alpha(0.01, 0.05, 0.1) addtext(Avg. age X year FE,  YES, HH size X year FE, YES )   
local replace append
}

global covs1 "age_m spanish_m schooling_m age spanish schooling hhmale urban np nchild5 age_sac  c.urba#i.year c.age_sac#i.year c.np#i.year i.departamento#i.year  i.year  nbi c.( nbi)#c.Post c.(nbi)#i.year i.min_schooling#c.(nbi)"
local spec1  Post c.Post#c.Top Top c.Top#i.year i.min_schooling i.min_schooling#c.Top  $covs1  
local sample1 "(min_schooling>=1 & min_schooling<9)"

foreach x in work_m w_hoursw_m  w_ylabor_m informal4_m w_rev_m w_costs_m any_costs_m {
sum `x' [w=factor]  if tau==-1 & `sample1' & Top==0
local mean_dv=r(mean)
areg `x'  `spec1'  [w=factor] if `sample1',a(departamento)cluster(municipio)
lincom _b[Post]+_b[c.Post#c.Top]
local bhigh=`r(estimate)'
test Post+c.Post#c.Top=0
local phigh=`r(p)'
outreg2 using "$dir3\PTable5r", excel `replace' ctitle(``x'_lab') keep(`parm' c.nbi#c.Post) less(0) nocons  bdec(3) cttop(`top`y'') adds(Clusters, e(N_clust),Mean DV, `mean_dv', Effect High Access, `bhigh', P-val High Access, `phigh' ) alpha(0.01, 0.05, 0.1) addtext(Avg. age X year FE,  YES, HH size X year FE, YES )   
local replace append
}

global covs1 "age_p spanish_p schooling_p age spanish schooling hhmale urban np nchild5 age_sac c.urba#i.year  c.age_sac#i.year c.np#i.year   i.departamento#i.year  i.year  nbi c.(nbi)#c.Post c.(nbi)#i.year i.min_schooling#c.( nbi)"
local spec1  Post c.Post#c.Top Top c.Top#i.year i.min_schooling i.min_schooling#c.Top  $covs1  
local sample1 "(min_schooling>=1 & min_schooling<9)"
g work_60=hoursw_p>60
replace work_60=. if hoursw_p==.
foreach x in work_p w_hoursw_p  w_ylabor_p  informal4_p w_rev_p w_costs_p any_costs_p {
sum `x'  [w=factor] if tau==-1 & `sample1' & Top==0
local mean_dv=r(mean)
areg `x'  `spec1'  [w=factor] if `sample1',a(departamento) cluster(municipio)
lincom _b[Post]+_b[c.Post#c.Top]
local bhigh=`r(estimate)'
test Post+c.Post#c.Top=0
local phigh=`r(p)'
outreg2 using "$dir3\PTable5r", excel `replace' ctitle(``x'_lab') keep(`parm' c.nbi#c.Post) less(0) nocons  bdec(3) cttop(`top`y'') adds(Clusters, e(N_clust),Mean DV, `mean_dv', Effect High Access, `bhigh', P-val High Access, `phigh' ) alpha(0.01, 0.05, 0.1) addtext(Avg. age X year FE,  YES, HH size X year FE, YES )   
local replace append
}

/*

global covs1 "  age spanish schooling hhmale urban np nchild5 age_sac  c.age_sac#i.year c.np#i.year  c.urba#i.year i.departamento#i.year i.year nbi c.(nbi)#c.Post c.(urban nbi)#i.year i.min_schooling#c.(urban nbi)"
local spec1  Post c.Post#c.Top Top c.Top#i.year i.min_schooling i.min_schooling#c.Top  $covs1  
local sample1 "(min_schooling>=1 & min_schooling<9)"

local replace replace
foreach x in  work_head w_hoursw_head w_ylabor_head informal4_head  {
sum `x'  [w=factor] if tau==-1 & `sample1' & Top==0
local mean_dv=r(mean)
areg `x'  `spec1' [w=factor]  if `sample1',a(departamento)cluster(municipio)
lincom _b[Post]+_b[c.Post#c.Top]
local bhigh=`r(estimate)'
test Post+c.Post#c.Top=0
local phigh=`r(p)'
outreg2 using "$dir3\PTable5r", excel `replace' ctitle(``x'_lab') keep(`parm' ) less(0) nocons  bdec(3) cttop(`top`y'') adds(Clusters, e(N_clust),Mean DV, `mean_dv', Effect High Access, `bhigh', P-val High Access, `phigh' ) alpha(0.01, 0.05, 0.1) addtext(Avg. age X year FE,  YES, HH size X year FE, YES )   
local replace append
}

global covs1 "age_m spanish_m schooling_m age spanish schooling hhmale urban np nchild5 age_sac  c.urba#i.year c.age_sac#i.year c.np#i.year i.departamento#i.year  i.year  nbi c.(urban nbi)#c.Post c.(urban nbi)#i.year i.min_schooling#c.(urban nbi)"
local spec1  Post c.Post#c.Top Top c.Top#i.year i.min_schooling i.min_schooling#c.Top  $covs1  
local sample1 "(min_schooling>=1 & min_schooling<9)"

foreach x in work_m w_hoursw_m  w_ylabor_m informal4_m  {
sum `x' [w=factor]  if tau==-1 & `sample1' & Top==0
local mean_dv=r(mean)
areg `x'  `spec1'  [w=factor] if `sample1',a(departamento)cluster(municipio)
lincom _b[Post]+_b[c.Post#c.Top]
local bhigh=`r(estimate)'
test Post+c.Post#c.Top=0
local phigh=`r(p)'
outreg2 using "$dir3\PTable5r", excel `replace' ctitle(``x'_lab') keep(`parm' ) less(0) nocons  bdec(3) cttop(`top`y'') adds(Clusters, e(N_clust),Mean DV, `mean_dv', Effect High Access, `bhigh', P-val High Access, `phigh' ) alpha(0.01, 0.05, 0.1) addtext(Avg. age X year FE,  YES, HH size X year FE, YES )   
local replace append
}

global covs1 "age_p spanish_p schooling_p age spanish schooling hhmale urban np nchild5 age_sac c.urba#i.year  c.age_sac#i.year c.np#i.year   i.departamento#i.year  i.year  nbi c.(urban nbi)#c.Post c.(urban nbi)#i.year i.min_schooling#c.(urban nbi)"
local spec1  Post c.Post#c.Top Top c.Top#i.year i.min_schooling i.min_schooling#c.Top  $covs1  
local sample1 "(min_schooling>=1 & min_schooling<9)"
g work_60=hoursw_p>60
replace work_60=. if hoursw_p==.
foreach x in work_p w_hoursw_p  w_ylabor_p  informal4_p  {
sum `x'  [w=factor] if tau==-1 & `sample1' & Top==0
local mean_dv=r(mean)
areg `x'  `spec1'  [w=factor] if `sample1',a(departamento) cluster(municipio)
lincom _b[Post]+_b[c.Post#c.Top]
local bhigh=`r(estimate)'
test Post+c.Post#c.Top=0
local phigh=`r(p)'
outreg2 using "$dir3\PTable5r", excel `replace' ctitle(``x'_lab') keep(`parm' ) less(0) nocons  bdec(3) cttop(`top`y'') adds(Clusters, e(N_clust),Mean DV, `mean_dv', Effect High Access, `bhigh', P-val High Access, `phigh' ) alpha(0.01, 0.05, 0.1) addtext(Avg. age X year FE,  YES, HH size X year FE, YES )   
local replace append
}
